package gui;

import javafx.application.Application;
import javafx.beans.binding.StringBinding;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;
import javafx.stage.Window;
import debugging.Logger;

/**
 * The Main GUI which is responsible for the interaction between the system and the user
 * @author Etienne Bruines
 * @author Bas Dobbelsteen
 */
public class Main extends Application
{
	private static Stage mainStage;
	private static Scene mainScene; 
	private static GridPane mainGridPane;
	
	private static Logger mainLogger;
	private static Settings mainSettings;
	private static ImageManager mainImageManager;
	private static CursorManager mainCursorManager;
	private static ModeManager mainModeManager;
	private static ToolBar mainToolBar;
	private static ToolBarRight rightToolBar;
	private static MenuBar mainMenuBar;
	private static KTimer mainKTimer;
	
	private static final String TITLE = "Prototype";
	private static final String FAVICON = "favicon.png";
	private static final String BACKGROUND = "#a1a1a1";
	private static final int WIDTH = 1024; 
	private static final int HEIGHT = 700;
	
	/**
	 * Called whenever the GUI starts / Main.launch()
	 */
	@Override public void start(Stage arg0) throws Exception 
	{
		mainLogger = new Logger(true);
		mainSettings = new Settings();
		mainKTimer = new KTimer();
		
		mainImageManager = new ImageManager(mainSettings);
		mainToolBar = new ToolBar(mainSettings);
		rightToolBar = new ToolBarRight(mainSettings, mainImageManager);
		
		mainMenuBar = new MenuBar();
		mainModeManager = new ModeManager(mainSettings, mainImageManager);
		
		mainStage = arg0;
		mainStage.setTitle(TITLE);
		mainStage.setWidth(WIDTH);
		mainStage.setHeight(HEIGHT);
		mainStage.getIcons().add(new Image(Main.class.getResource(FAVICON).toExternalForm()));
		
		mainGridPane = new GridPane();
		mainGridPane.setPrefWidth(WIDTH);
		mainGridPane.add(mainMenuBar, 0, 0, 3, 1);
		mainGridPane.add(mainToolBar, 0, 1);
		mainGridPane.add(rightToolBar, 2, 1);
		mainGridPane.add(mainImageManager.getTabPane(), 1, 1);
		
		RowConstraints rowConstraint1 = new RowConstraints(mainMenuBar.getPrefHeight());
		RowConstraints rowConstraint2 = new RowConstraints(HEIGHT - mainMenuBar.getPrefHeight() - 63);
		mainGridPane.getRowConstraints().addAll(rowConstraint1, rowConstraint2);
		
		ColumnConstraints colConstraint1 = new ColumnConstraints(mainToolBar.getPrefWidth());
		ColumnConstraints colConstraint2 = new ColumnConstraints(WIDTH - mainToolBar.getPrefWidth() - rightToolBar.getPrefWidth() - 16);
		ColumnConstraints colConstraint3 = new ColumnConstraints(rightToolBar.getPrefWidth());
		mainGridPane.getColumnConstraints().addAll(colConstraint1, colConstraint2, colConstraint3);
		
		mainScene = new Scene(mainGridPane );
		
		mainStage.setScene(mainScene);
		
		mainCursorManager = new CursorManager(mainSettings, mainScene);
		
		mainStage.show();
		mainSettings.setMode(Mode.None);
		
		StringBinding titleBinding = new StringBinding()
		{
			{
				super.bind(mainKTimer.timeStringProperty(), mainKTimer.startedProperty());
			}

			@Override protected String computeValue() 
			{
				return (mainKTimer.startedProperty().get() ? "Task Active" : "Task Inactive") + (mainKTimer.getTime() > 0 ? " - elapsed time: " + mainKTimer.timeStringProperty().get() : "");
			}
		};
		mainStage.titleProperty().bind(titleBinding);
		
		mainLogger.logLine("Graphical User Interface - started");
	}
	
	// ============
	// Event handlers
	// ============
	
	
	
	// ============
	// Getters
	// ============
	
	public static Logger getLogger()
	{
		return mainLogger;
	}
	
	public static Settings getSettings()
	{
		return mainSettings;
	}
	
	public static String getTitle()
	{
		return TITLE;
	}
	
	public static String getBackground()
	{
		return BACKGROUND;
	}
	
	public static ToolBar getToolBar()
	{
		return mainToolBar;
	}
	
	public static ImageManager getImageManager()
	{
		return mainImageManager;
	}
	
	public static Window getWindow()
	{
		return mainStage;
	}
	
	public static KTimer getKTimer()
	{
		return mainKTimer;
	}
	
	public static void main (String ... args )
	{
		Main.launch();
	}

}
